//
//  HttpUtil.m
//  GroupBuy
//
//  Created by wwj on 15/11/10.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "HttpUtil.h"
#import "DPAPI.h"

@interface HttpUtil ()<DPRequestDelegate>

@property (strong, nonatomic) NSMutableDictionary *dictBlock;

@end

@implementation HttpUtil

-(NSMutableDictionary *)dictBlock{
    if (_dictBlock == nil) {
        _dictBlock = [NSMutableDictionary dictionary];
    }
    return _dictBlock;
}


+(instancetype)shareHttpUtil{
    static HttpUtil *httpUtil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        httpUtil = [[HttpUtil alloc] init];
    });
    return httpUtil;
}

-(void)requestWithURL:(NSString *)url params:(NSDictionary *)params block:(RequestBlock)block{
    DPAPI *api = [DPAPI shareDPAPI];
    DPRequest *request = [api requestWithURL:url params:params delegate:self];
    [self.dictBlock setObject:block forKey:request.description];
}

- (void)request:(DPRequest *)request didFinishLoadingWithResult:(id)result{
    RequestBlock block = _dictBlock[request.description];
    if (block) {
        block(result, nil);
    }
}

-(void)request:(DPRequest *)request didFailWithError:(NSError *)error{
    RequestBlock block = _dictBlock[request.description];
    if (block) {
        block(nil, error);
    }
}
@end
